import java.util.Arrays;

public class Solution1299 {
    public int[] replaceElements(int[] arr) {
        int[] ans=new int[arr.length];
        ans[arr.length-1]=-1;
        int max=arr[arr.length-1];
        for (int i=arr.length-2;i>=0;i--){
            ans[i]=max;
            max=Math.max(max,arr[i]);
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(new Solution1299().replaceElements(new int[]{17, 18, 5, 4, 6, 1})));
    }
}
